//
//  BKEXGoodsDetailCell.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/3/23.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKEXGoodsDetailCell.h"

#import "BKMainCommon.h"
#import "BKExchangeGoodsModel.h"
#import "BK_OD_Color.h"


#define kMainLabelCorol RGB(64, 64, 64)
#define COLOR_ORDER_DETAIL_Line RGB(218, 218, 218)
#define BESTKEEP_RED RGB(255, 16, 16)

@interface BKEXGoodsDetailCell()

@property (nonatomic, strong) UILabel *mainNameLabel;
@property (nonatomic, strong) UILabel *scoresLabel;
@property (nonatomic, strong) UILabel *goodsNumber;
@property (nonatomic, strong) UIImageView *rightImageView;
@property (nonatomic, strong) UIImageView *lineView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *infoLabel;
@property (nonatomic, strong) UILabel *explainBottomLabel;

@end
@implementation BKEXGoodsDetailCell
- (UILabel *)mainNameLabel {
    
    if (!_mainNameLabel) {
        _mainNameLabel = [[UILabel alloc] init];
        _mainNameLabel.font = [UIFont systemFontOfSize:18];
        _mainNameLabel.numberOfLines = 2;
        _mainNameLabel.textAlignment = NSTextAlignmentLeft;
        _mainNameLabel.textColor = kMainLabelCorol;
        [self addSubview:_mainNameLabel];
        
    }
    return _mainNameLabel;
}
- (UILabel *)scoresLabel {
    
    if (!_scoresLabel) {
        _scoresLabel = [[UILabel alloc] init];
        [_scoresLabel setTextAlignment:NSTextAlignmentLeft];
        [self addSubview:_scoresLabel];
    }
    return _scoresLabel;
}
- (UILabel *)goodsNumber {
    
    if (!_goodsNumber) {
        _goodsNumber = [[UILabel alloc] init];
        _goodsNumber.textAlignment = NSTextAlignmentLeft;
        _goodsNumber.textColor = RGB(136, 136, 136);
        _goodsNumber.font = [UIFont systemFontOfSize:14];
        [self addSubview:_goodsNumber];
    }
    return _goodsNumber;
}
- (UIImageView *)rightImageView {
    
    if (!_rightImageView) {
        _rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"order_right_arrow"]];
        [self addSubview:_rightImageView];
    }
    return _rightImageView;
}
- (UIImageView *)lineView {
    
    if (!_lineView) {
        _lineView = [[UIImageView alloc] init];
        _lineView.backgroundColor = COLOR_ORDER_DETAIL_Line;
        [self addSubview:_lineView];
    }
    return _lineView;
}
- (UILabel *)titleLabel {
    
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        [_titleLabel setTextAlignment:NSTextAlignmentLeft];
        _titleLabel.textColor = kOrderBlack1;
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}
- (UILabel *)infoLabel {
    
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc] init];
        _infoLabel.numberOfLines = 0;
        _infoLabel.textColor = kOrderBlack1;
        _infoLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_infoLabel];
    }
    return _infoLabel;
}
- (UILabel *)explainBottomLabel {
    
    if (!_explainBottomLabel) {
        _explainBottomLabel = [[UILabel alloc] init];
        _explainBottomLabel.textColor = RGB(172, 172, 172);
        [_explainBottomLabel setTextAlignment:NSTextAlignmentCenter];
        _explainBottomLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:_explainBottomLabel];
    }
    return _explainBottomLabel;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)getInfoGoods:(BKExchangeGoodsModel *)model cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        self.titleLabel.text = @"兑换说明:";
        self.titleLabel.font=[UIFont boldSystemFontOfSize:16];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(20);
            make.top.mas_equalTo(self.mas_top).offset(15);
            make.height.mas_equalTo(20);
        }];
        NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle1 setLineSpacing:4];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:model.explain attributes:@{NSParagraphStyleAttributeName:paragraphStyle1}];
        self.infoLabel.attributedText = attributedString;
        [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(20);
            make.right.mas_equalTo(self.mas_right).offset(-10);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(15);
        }];

}










- (void)getString:(BKExchangeGoodsModel *)model AttributedString:(UILabel *)label {
    
    NSString *string;
    if (model.needMoney.length) {
        string = [NSString stringWithFormat:@"%@+%@ %@",model.needIntegral,model.needMoney,model.goodsPrice];
    }else {
        string = [NSString stringWithFormat:@"%@ %@",model.needIntegral,model.goodsPrice];
    }
    
    string = [string stringByReplacingOccurrencesOfString:@"￥" withString:@"¥"];
    /**
     NSMutableAttributedString 初始化
     */
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:10]}];
    
    //第一部分的添加
    [attributedString addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:36],NSForegroundColorAttributeName:kMainLabelCorol} range:NSMakeRange(0, model.needIntegral.length)];
    
    if (model.needMoney.length) {
        //加号属性添加BESTKEEP_RED
        [attributedString addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:kMainLabelCorol} range:NSMakeRange(model.needIntegral.length, 1)];
        
        //需付款添加
        [attributedString addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:BESTKEEP_RED} range:NSMakeRange(model.needIntegral.length+1, model.needMoney.length)];
        
        //给最后一个添加
        [attributedString addAttributes:@{NSStrikethroughStyleAttributeName:@(1),NSStrikethroughColorAttributeName:RGB(178, 178, 178),NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:RGB(178, 178, 178)} range:NSMakeRange(model.needIntegral.length+2+model.needMoney.length, model.goodsPrice.length)];
        
    }else {
        //给最后一个添加
        [attributedString addAttributes:@{NSStrikethroughStyleAttributeName:@(1),NSStrikethroughColorAttributeName:RGB(178, 178, 178),NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:RGB(178, 178, 178)} range:NSMakeRange(model.needIntegral.length+1, model.goodsPrice.length)];
        
    }
    
    [attributedString addAttributes:@{NSStrikethroughStyleAttributeName:@(1),NSStrikethroughColorAttributeName:[UIColor clearColor]} range:NSMakeRange(1, 1)];
    
    label.attributedText = attributedString;
}

+ (CGFloat)getCellHeightFromInfo:(BKExchangeGoodsModel *)model heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UIFont *font;
    CGSize titleSize;
    
    CGSize size = CGSizeMake(ScreenWidth-30.0f, MAXFLOAT);
    font = [UIFont systemFontOfSize:14];
    titleSize = [model.explain boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:font} context:nil].size;
    if (iPhone4s  || iPhone5) {
        titleSize.height = titleSize.height + 111;
    }else {
        titleSize.height = titleSize.height ;
    }
    
    return titleSize.height;
}

//换行加空格
+ (NSString *)lineFeedAddSpace:(CGSize)size withString:(NSString *)str {
    
    NSArray *tempArray = [str componentsSeparatedByString:@"；"];
    NSString *mainStr;
    UIFont *font = [UIFont systemFontOfSize:14];
    for (NSString *string in tempArray) {
        
        NSString *totalStr = [string copy];
        NSMutableString *tempMutStr = [string mutableCopy];
        CGSize titleSize;
        NSString *tempStr;
        CGSize tempSize;
        for (int i = 1; i <= totalStr.length; i++) {
            tempStr = [totalStr substringToIndex:i];
            titleSize = [tempStr boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSLineBreakByWordWrapping attributes:@{NSFontAttributeName:font} context:nil].size;
            if (titleSize.height > tempSize.height+5) {
                if (tempSize.height != 0) {
                    [tempMutStr insertString:@"  " atIndex:i-1];
                    totalStr = [NSString stringWithFormat:@"%@",tempMutStr];
                }
                tempSize = titleSize;
            }
        }
        if (mainStr) {
            mainStr = [NSString stringWithFormat:@"%@\n%@",mainStr,totalStr];
        }else {
            mainStr = [totalStr copy];
        }
    }
    return mainStr;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end












































